---
title: "Table 3"
author: "Yingjie Fan"
date: "2023-06-02"
---

```{r echo=FALSE, message=FALSE, warning=FALSE}
rm(list=ls())
#install.packages(c("dplyr", "tidyr", "ggplot2", "showtext", "likert", "data.table", "zoo", "ggrepel", "Hmisc"))
#Load Packages
library(dplyr)
library(tidyr)
library(ggplot2)
library(showtext)
library(likert)
library(data.table)
library(zoo)
library(ggrepel)
library(Hmisc)
path = "" # Insert path
```


```{r Figure S1: Daily Historical Followers (Social Blade Data)}
### Generate the figures for follower count data for non-Chinese outlets + CGTN
follow_foriegn<-fread(paste0(path,"/Data/Followers/follower.csv")) %>%
  filter(username %in% c("CGTN","RT","Al Jazeera","BBC Breaking","CNN Breaking"))%>%
 ggplot(aes(x=as.Date(date), y=followers,linetype=username)) +  
 geom_line()+
  expand_limits(x=as.Date("2020-08-01"))+
   scale_x_date(date_breaks = 'year', date_labels = '%Y')+
  geom_text(aes(label=ifelse(date=="2020-01-10",as.character(username),'')),hjust=0,vjust=-0.5)+
  labs(y="Followership (in Millions)", x = "",title="")+ 
  theme_bw()+
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "RT" = "dashed","Al Jazeera" = "twodash","BBC Breaking" = "dotted","CNN Breaking" = "longdash")) +
  guides(scale = "none")
ggsave(paste0(path,"/Output/figureS1_daily_follower_social_blade.png"),follow_foriegn,width=7, height=5)
```

```{r Figure S2: Estimated Number of Followers (Twitter Follower List)}
#Data source: Twitter does not provide the exact date and time at which one account began following another account, but we can infer an earliest possible following date because an account’s list of followers is ordered according to date and Twitter metadata on follower objects come with a creation timestamp. We estimated the follower number from the full list of followers for each account. 

follower_2 = fread(paste0(path,"/Data/Followers/follower_estimation.csv"))
follow_foriegn_2<-follower_2 %>%
 ggplot(aes(x=as.Date(date), y=cumsum/1000000,linetype=username)) +  
 geom_line()+
  expand_limits(x=as.Date("2021-02-01"))+
   scale_x_date(date_breaks = 'year', date_labels = '%Y')+
  geom_text(aes(label=ifelse(date=="2020-01-10",as.character(username),'')),hjust=0,vjust=-0.5)+
  labs(y="Followership (in Millions)", x = "",title="")+ 
  theme_bw()+
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "RT" = "dashed","Al Jazeera" = "twodash","BBC Breaking" = "dotted","CNN Breaking" = "longdash")) +guides(scale = "none")
ggsave(paste0(path,"/Output/figureS2_daily_follower_est.png"),follow_foriegn_2,width=7, height=5)
```

```{r Figure S3 Bot: No Timeline}
error_sum_full=fread(paste0(path,"/Data/Bots/error.csv"))

error_sum_full$month<-as.Date(as.yearmon(error_sum_full$month))

error_p<- error_sum_full %>%
  filter(error_type=="No timeline")%>%
  ggplot(aes(x=as.Date(month),y=ratio, linetype=following_acct))+
  scale_linetype_manual(values=c("BBC Breaking"="dotted", "CGTN"="solid","CNN Breaking"="dashed"))+
  geom_line()+
  scale_color_grey()+
  theme_bw()+
  theme(legend.position = c(0.15, 0.85),
        legend.title = element_blank())+
  xlim(as.Date(c('2013-01-01','2019-12-01')))+
  labs(x="Account Join Date",y="Proportion of followers with no timeline", title="Proportion of followers with no timeline")

ggsave(paste0(path,"/Output/figureS3_bot_error.png"),error_p,width=7, height=5)
```

```{r Figure S4 Bot: CAP}
bot_ci=fread(paste0(path,"/Data/Bots/bot_ci.csv"),drop = T)
bot_3_cap<-bot_ci%>%
  mutate(month = as.yearmon(month))%>%
ggplot(aes(x=month, y=Mean,linetype=following_acct))+
  geom_line(aes(group=following_acct))+
  geom_ribbon(aes(x=month, y=Mean, ymax=Lower, ymin=Upper), 
              alpha=0.2)+
     expand_limits(x=as.yearmon("Jul 2021"))+
   scale_x_yearmon(format = "%Y")+
  ylim(0,0.1)+
  theme_bw()+
  labs(x="Account Join Date",y="Proportion of accounts with CAP above 95%")+
  theme(legend.position = c(0.15, 0.85),
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "BBC Breaking" = "dotted","CNN Breaking" = "dashed")) 
ggsave(paste0(path,"/Output/figureS4_bot_cap.png"),bot_3_cap,width=7, height=5)
```


```{r Processing to generate the engagement data}
# tweets = fread(paste0(path,"/Tweets/tweets.csv"))
# 
# engagement = tweets %>%
#   select(username,timestamp,retweet,reply,like)%>%
#   gather(metric, value, 3:5) %>%
#   mutate(metric = dplyr::recode(metric, "like" = "Likes", "reply" = "Replies", "retweet" = "Retweets"))%>%
#   mutate(D=ifelse(timestamp>"2017-07-01",1,0)) %>%
#     mutate(month=as.yearmon(substr(timestamp,1,7)))%>%
#   group_by(username,metric,D)%>%
#   summarise(value=median(value))
# 
# write.csv(engagement,file=paste0(path,"/Tweets/engagement_metrics.csv"),fileEncoding = "UTF-8")
```

```{r Figure S5: Engagement of tweets}
engage = fread(paste0(path,"/Data/Tweets/engagement_metrics.csv"))

engage_p = engage %>%
  ggplot(aes(x = forcats::fct_rev(factor(username,levels = c('CGTN', 'China Daily','People\'s Daily','Xinhua', 'Al Jazeera', 'BBC Breaking', 'CNN Breaking', 'RT'))), y= value, group = forcats::fct_rev(as.factor(D)),fill = as.factor(D))) +
  geom_bar(stat="identity", width=.8, position = "dodge") +
  scale_fill_grey(start = 0.4, end = .7,labels = c("Pre 2017", "Post 2017"))+
  facet_wrap(~metric)+theme_bw()+coord_flip()+geom_text(aes(label=round(value)), position = position_dodge(width = .9),hjust =-0.25,size = 3) +
  ylim(0,800)+
  labs(y="Median Engagement per Tweet", x = "",title="")+
  theme(legend.position = "bottom",legend.title = element_blank(),legend.text=element_text(size=10), axis.text.x = element_text(size=8),axis.text.y = element_text(size=10),axis.title=element_text(size=10))

ggsave(paste0(path,"/Output/figureS5_engagement.png"),engage_p,width=7, height=5)

```
